<?php

namespace app\backstage\validate;

use think\Validate;

class QuickAppValidate extends Validate
{
    protected $rule = [
        'id' => 'require|number|gt:0',
        'ids' => 'require|array',
        'contact' => 'require|max:25',
        'package_name' => 'require|max:50',
        'subject_id' => 'number|egt:0',
        'remarks' => 'max:100',
        'mobile_brand' => 'require|in:show,huawei,xiaomi,OPPO,vivo',
        'domain' => 'url',
        'address' => 'max:60',
        'api_version' => 'require',
        'provider' => 'in:huawei,xiaomi,OPPO,vivo',
        'quick_id' => 'require|number|egt:1',
        'status' => 'in:0,1',
        'desc' => 'max:80',
        'field' => 'require|in:desc,status',
        'value' => 'require|in:0,1',
        'novel_ids' => 'require|checkIds',
        'switch' => 'in:1',
        'iplib_ids' => 'checkIds',
        'white_uids' => 'checkIds',
        'open_hour' => 'array',
        'link_replace_nid' => 'require|number|gt:0',
        'title' => 'require|max:25',
        'ips' => 'require',
        'lib_id' => 'require|number|gt:0',
        'wx_payment_id' => 'number|egt:0',
        'zfb_payment_id' => 'number|egt:0',
        'sms_sign' => 'max:15',
        'protect_day' => 'require|number|egt:0',
        'qids' => 'require|checkIds',
    ];

    protected $message = [
        'domain.url' => '域名参数异常',
        'desc.max' => '备注长度过长！',
        'quick_id.require' => '请选择App',
        'novel_ids.require' => '小说id必填',
    ];

    protected $scene = [
        'changeContact' => ['ids', 'contact'],
        'sendConfigForm' => ['id', 'mobile_brand'],
        'editRemarks' => ['id', 'remarks'],
        'addExamineNovel' => ['quick_id', 'novel_ids'],
        'setBlockRule' => ['id', 'link_replace_nid', 'switch', 'iplib_ids', 'white_uids', 'open_hour'],
        'setBlockRulePl' => ['qids', 'link_replace_nid', 'switch', 'iplib_ids', 'open_hour'],
        'addLibIp' => ['lib_id', 'ips'],
    ];

    // 自定义验证规则
    protected function checkIds($value, $rule, $data = [])
    {
        if (is_array($value)) {
            $value = implode(',', $value);
        }
        if (!preg_match('#^[1-9][0-9]*(,[1-9][0-9]*)*$#', $value)) {
            return '参数异常，请检查是否为正确的id，并且格式是否正确';
        }
        return true;
    }


    public function sceneSetQuickApp()
    {
        return $this->only(['id', 'package_name', 'subject_id', 'remarks', 'wx_payment_id', 'zfb_payment_id'])
            ->remove('id', 'require');
    }


    public function sceneSetSubject()
    {
        return $this->only(['id', 'domain', 'contact', 'address', 'remarks'])
            ->remove('id', 'require')
            ->remove('contact', 'require');
    }

    public function sceneAddAuditEvent()
    {
        return $this->only(['api_version', 'provider', 'quick_id', 'status', 'desc']);
    }


    public function sceneSetIpLib()
    {
        return $this->only(['id','title'])
            ->remove('id', 'require');
    }


    public function sceneDelAuditEvent()
    {
        return $this->only(['id']);
    }


    public function sceneEditAuditEventField()
    {
        return $this->only(['id', 'field']);
    }

    public function sceneDesc()
    {
        return $this->only(['value'])
            ->remove('value', 'require|in')
            ->append('value', 'max:80');
    }

    public function sceneStatus()
    {
        return $this->only(['value']);
    }

    public function sceneSetAuditorStatus()
    {
        return $this->only(['id', 'status'])
            ->append('status', 'require');
    }
}